//
//  GPChatViewModel.m
//  GuaPi
//
//  Created by 陈华 on 2017/6/22.
//  Copyright © 2017年 Joanlove. All rights reserved.
//

#import "GPChatViewModel.h"

@implementation GPChatViewModel

/**快速创建*/
- (instancetype)initWithChatType:(EMChatType)chatType conversationId:(NSString *)conversationId toId:(NSString *)toId toNickName:(NSString *)toNickName toAvatarURLPath:(NSString *)toAvatarURLPath
{
    return [self initWithChatType:chatType userId:nil conversationId:conversationId toId:toId toNickName:toNickName toAvatarURLPath:toAvatarURLPath];
}

/**快速创建*/
- (instancetype)initWithChatType:(EMChatType )chatType
                          userId:(NSString *)uid
                  conversationId:(NSString *)conversationId
                            toId:(NSString *)toId
                      toNickName:(NSString *)toNickName
                 toAvatarURLPath:(NSString *)toAvatarURLPath
{
    if (self = [super init]) {
        
        self.chatType = chatType;
        self.uid = uid;
        self.conversationId = conversationId;
        self.toId = toId;
        self.toNickname = toNickName;
        self.toAvatarURLPath = toAvatarURLPath;
        
    }
    return self;
}



#pragma mark - 快速发送SOS紧急消息
- (void)sendSOSMessage:(UIImage *)originImage latitude:(double)latitude longitude:(double)longitude address:(NSString *)address
{
    //构造图片消息消息体
    NSData *originData = UIImagePNGRepresentation(originImage);
    EMImageMessageBody *body = [[EMImageMessageBody alloc] initWithData:originData displayName:@"image.png"];
    
    //生成message
    NSString *from = [[EMClient sharedClient] currentUsername];
    EMMessage *message = [[EMMessage alloc] initWithConversationID:self.conversationId from:from to:self.toId body:body ext:nil];
    message.chatType = self.chatType;
    message.ext = @{@"address":address,
                    @"latitude":@(latitude),
                    @"longitude":@(longitude)};
    
    //发送消息
    [[EMClient sharedClient].chatManager sendMessage:message progress:nil completion:nil];

}

/**发送SOS定位消息*/
- (void)sendSOSLocationMessage:(NSString *)address latitude:(double)latitude longitude:(double)longitude {
    //创建定位消息消息体
    EMLocationMessageBody *body = [[EMLocationMessageBody alloc] initWithLatitude:latitude longitude:longitude address:address];
    
    //生成message
    NSString *from = [[EMClient sharedClient] currentUsername];
    EMMessage *message = [[EMMessage alloc] initWithConversationID:self.conversationId from:from to:self.toId body:body ext:nil];
    message.chatType = self.chatType;
    message.ext = @{@"name":@"SOS"};
    
    //发送消息
    [[EMClient sharedClient].chatManager sendMessage:message progress:nil completion:nil];
}

/**发送SOS测试文本消息*/
- (void)sendSOSTestMessage:(NSString *)sosTest
{
    //构造文字消息消息体
    EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:sosTest];
    
    //生成Message
    NSString *from = [[EMClient sharedClient] currentUsername];
    EMMessage *message = [[EMMessage alloc] initWithConversationID:self.conversationId from:from to:self.toId body:body ext:nil];
    message.chatType = self.chatType;
    
    //发送消息
    [[EMClient sharedClient].chatManager sendMessage:message progress:nil completion:nil];
}

/**快速发送图片消息*/
- (void)sendImageMessage:(UIImage *)originImage
{
    //构造图片消息消息体
    NSData *originData = UIImagePNGRepresentation(originImage);
    EMImageMessageBody *body = [[EMImageMessageBody alloc] initWithData:originData displayName:@"image.png"];
    
    //生成message
    NSString *from = [[EMClient sharedClient] currentUsername];
    EMMessage *message = [[EMMessage alloc] initWithConversationID:self.conversationId from:from to:self.toId body:body ext:nil];
    message.chatType = self.chatType;
    
    //发送消息
    [[EMClient sharedClient].chatManager sendMessage:message progress:nil completion:nil];

}

@end
